package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Myemp;
import com.hopu.service.EmpService;
import com.hopu.service.Impl.EmpServiceImpl;
import com.hopu.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/empp")
public class EmpConreoller {
    @Autowired
    private EmpService empService;

    @RequestMapping("/login")
    @ResponseBody
    public ResultUtils login(@RequestParam String ename, @RequestParam String epsw, HttpServletRequest request){
        Myemp login = empService.login(ename, epsw);
        System.out.println(login);
        if(login==null){
            return new ResultUtils(400,"n",null);
        }else {
//            request.getSession().setAttribute("session",login);
            return new ResultUtils(200,"y",login);
        }

    }


    @RequestMapping("/findAll")
    @ResponseBody
    public ResultUtils findAll(@RequestParam(value = "pn",defaultValue = "1")Integer pn,@RequestParam(value = "eflag")Integer eflag){
        PageHelper.startPage(pn,14);
        List<Myemp> all = empService.findAll(eflag);
        PageInfo page = new PageInfo(all,5);
        return new ResultUtils(200,"y",page);

    }

    @RequestMapping("/falsedelete")
    @ResponseBody
    public ResultUtils falsedelete(@RequestParam Integer eid,@RequestParam Integer eflag){
        System.out.println(eflag);
        if (eflag==1){
            eflag=0;
            System.out.println("if            " + eflag);
            empService.falsedelete(eid,eflag);
            return new ResultUtils(200,"yeah",null);
        }
        eflag =1;
        System.out.println("else         "+eflag);
        empService.falsedelete(eid,eflag);
        return new ResultUtils(200,"yeah",null);

    }

    @RequestMapping("/insertemp")
    @ResponseBody
    public ResultUtils insertemp(Myemp myemp){
        Integer insertemp = empService.insertemp(myemp);
        return new ResultUtils(200,"y",null);

    }

    @RequestMapping("/findByid")
    @ResponseBody
    public ResultUtils findByid(@RequestParam Integer eid){
        Myemp byId = empService.findById(eid);
        return new ResultUtils(200,"y",byId);
    }
    @RequestMapping("findByEname")
    private ResultUtils findByEname(@RequestParam("ename")String ename){

        Myemp emp = empService.findByEname(ename);
        if (emp!=null){
            System.out.println(emp);
            return new ResultUtils(200,"SUCCESS",emp);
        }
        return new ResultUtils(400,"Error",null);
    }

    @RequestMapping("/updateemp")
    @ResponseBody
    public ResultUtils updateemp(Myemp myemp){
        System.out.println(myemp);
        Integer insertemp = empService.updateEmp(myemp);
        System.out.println(insertemp);
        return new ResultUtils(200,"y",null);

    }
}
